package com.hua.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.entities.Employee;
import com.hua.entities.R;
import com.hua.service.EmployeeService;
import com.hua.service.serviceImpl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest httpServletRequest,@RequestBody Employee employee){

        //加密MD5
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);

        if (emp==null){
            return R.error("登陆失败,用户名不存在");
        }

        if(!emp.getPassword().equals(password)){
            return R.error("登录失败,密码错误");
        }
        if(emp.getStatus()==0){
            return R.error("登陆失败,该用户被禁用");
        }

        httpServletRequest.getSession().setAttribute("user",emp);

        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest servletRequest){
        servletRequest.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

    @PostMapping()
    public R<String> addEmployee(HttpServletRequest request , @RequestBody  Employee employee){
        log.info("新增员工");

        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        //LocalDateTime.now() 获得当前系统事件
        //公共抽取注入值
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Employee user = (Employee) request.getSession().getAttribute("user");
//        employee.setUpdateUser(user.getId());
//        employee.setCreateUser(user.getId());
        System.out.println(employee);
//        LambdaQueryWrapper<Employee> lambdaQueryWrapper =
//                new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Employee::getName,"陈俊华");
        //只针对这一个模块来捕获异常，但是我们一般不这么用，一般要全局捕获
//        try {
            employeeService.save(employee);
//        } catch (Exception e) {
//            return R.error("新增失败");
//        }
//        System.out.println(employeeService.getOne(lambdaQueryWrapper));
        return R.success("新增成功");
    }

    /**
     *
     * @param page 分多少页？
     * @param pageSize 总页数
     * @param name 根据名字查询
     * @return
     */
    @GetMapping("/page")
    public R<Page> queryAll(int page, int pageSize, String name){
        //利用这个注意添加对应的分页插件，至于page，pageSize，name则要根据前端的页面，看前端需要什么数据
        log.info("页数，页数大小，名字，{}，{}，{}",page,pageSize,name);
        Page pageWrapper = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,Employee::getName,name);
        employeeService.page(pageWrapper,lambdaQueryWrapper);
        return R.success(pageWrapper);
    }

    /**
     * 复用链接，修改和禁用按钮都用这个链接
     * @param request
     * @param employee
     * @return
     */
    @PutMapping()
    public R<String> updateEmployee(HttpServletRequest request,@RequestBody Employee employee){

        Employee user = (Employee) request.getSession().getAttribute("user");
//
//        employee.setUpdateUser(user.getId());
//        employee.setUpdateTime(LocalDateTime.now());

        employeeService.updateById(employee);
        System.out.println(employee);
        return R.success("修改信息成功~~~");
    }

    /**
     * 用于点编辑的时候给前端页面放回数据。
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> updateEmployee(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        System.out.println("111"+emp);
        if(emp!=null){
            return R.success(emp);
        }
        return R.error("操作失败");
    }
}
